<template>
  <div class="pagewrapper p-20">
    <div class="bg_fff pagebox h_full flex">
      <div class="pageleft flexcolumn h_full">
        <div class="p-16">
          <div class="c_text2 fw-6 mb-16">角色</div>
          <div class="flexbetween">
            <el-input
              v-model="search"
              size="small"
              placeholder="搜角色"
              suffix-icon="el-icon-search"
              class="searchinput"
            ></el-input>

            <div v-hasPermi="['system:role:add']" class="btnadd flexcenter commhover" @click="handleAdd">
              <el-tooltip
                class="item"
                effect="dark"
                content="添加角色"
                placement="top-start"
              >
                <i class="el-icon-plus f-16"></i>
              </el-tooltip>
            </div>
          </div>
        </div>
        <div class="left_body">
          <template v-if="roleList.length">
            <div
              class="role_item commhover flexbetween f-14"
              v-for="(item, index) in roleList"
              :key="index"
              :class="{ active: active == item.id }"
              @click="handleItem(item)"
            >
              <div class="flex aligncenter">
                <i class="el-icon-user mr-8 c_999"></i>
                <div class="role-title f-14">{{ item.roleName }}</div>
              </div>
              <el-dropdown @command="handleCommand($event, item)">
                <svg-icon
                  icon-class="more"
                  class-name="commhover roleicon f-15"
                />
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="a" v-hasPermi="['system:role:edit']">修改</el-dropdown-item>
                  <el-dropdown-item command="b" v-hasPermi="['system:role:delete']">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </template>
          <template v-else>
            <div class="flexcenter f-14 c_888999">暂无数据</div>
          </template>
        </div>
      </div>
      <div class="pageright">
        <div class="f-16 pl-16 fw-6 role-header flex aligncenter">功能名称</div>
        <role-main v-if="active" :rowid='active' />
      </div>
    </div>

    <el-dialog
      :title="title"
      :visible.sync="open"
      width="580px"
      :before-close="handleClose"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="160px">
        <el-form-item label="角色名称：" prop="name">
          <el-input
            v-model="form.name"
            size="small"
            maxlength="10"
            placeholder="请输入角色名称，不超过10个字"
            style="width: 266px"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="subloading"
          @click="handleSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getRoleList,
  addRole,
  updateRole,
  delRole,
  getRoleGnList,
  getRoleItem,
} from "@/api/role";
import RoleMain from "./RoleMain.vue";
export default {
  data() {
    return {
      search: "",
      roleList: [],
      active: "",
      title: "",
      open: false,
      form: {
        id: undefined,
        name: "",
        roleModel: "",
      },
      rules: {
        name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
      },
      gnlist: [],
      subloading: false,
      rowitem: null,
      keys:''
    };
  },
  components: {
    RoleMain,
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getRoleList().then((res) => {
        this.roleList = res.rows;
        this.handleItem(this.roleList[0]);
        this.loading = false;
      }).catch(()=>{
        this.loading = false;
      });
    },

    handleAdd() {
      this.title = "添加角色";
      this.getRoleGnList();
      this.open = true;
    },

    getRoleGnList() {
      getRoleGnList().then((res) => {
        this.gnlist = res.rows.filter((item) => item.parentId !== -1);
      });
    },

    handleClose() {
      this.open = false;
      this.rowitem = null;
      this.resetForm();
    },

    resetForm() {
      this.form = {
        id: undefined,
        name: "",
      };
    },

    handleEdit(item) {
      this.getRoleItem(item);
      this.title = "编辑角色";
      this.form.id = item.id;
      this.form.name = item.roleName;
      this.open = true;
    },

    // 获取角色详情
    getRoleItem(item) {
      getRoleItem(item.id).then((res) => {
        this.form.roleModel = res.data.roleModel;
      });
    },

    handleCommand(command, item) {
      console.log(item);
      switch (command) {
        case "a":
          this.handleEdit(item);
          break;
        case "b":
          this.handleDelete(item);
          break;
        default:
          break;
      }
    },

    handleDelete(item) {
      this.$confirm("确定删除该角色?", "", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showClose: false,
        iconClass: "el-icon-warning-outline",
        type: "warning",
      })
        .then(() => {
          delRole(item.id).then((res) => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },

    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        this.subloading = true;
        if (this.form.id) {
          updateRole(this.form).then(res=>{
            this.$message.success("编辑成功");
            this.handleClose();
            this.getList();
            this.subloading = false;
          }).catch(()=>{
            this.subloading = false;
          })
        } else {
          let ids = this.gnlist.map((item) => item.id);
          addRole({
            roleName: this.form.name,
            roleModel: ids.join(",")
          })
            .then((res) => {
              this.$message.success("添加成功");
              this.handleClose();
              this.getList();
              this.subloading = false;
            })
            .catch((e) => {
              this.subloading = false;
            });
        }
      });
    },

    handleItem(item) {
      this.active = item.id;
      this.keys = new Date().getTime()
    },
  },
};
</script>

<style lang="scss" scoped>
.pagebox {
  border-radius: 4px;
}
.pageleft {
  width: 240px;
  border-right: 1px solid #f0f0f0;
}
.left_body {
  height: calc(100% - 102px);
  overflow-y: auto;
  padding: 0 16px 16px;
}
.searchinput {
  width: 168px;
  // margin-right: ;
}
.btnadd {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  border: 1px solid #e9e9e9;
}
.role_item {
  width: 100%;
  border-radius: 4px;
  position: relative;
  height: 32px;
  padding: 0 12px;
  padding-right: 6px;
  margin-bottom: 4px;

  &.active {
    background-color: #ebf2ff;
  }
  &:hover {
    background-color: #f0f0f0;
  }

  .roleicon {
    display: none;
  }

  &:hover .roleicon {
    display: inline;
  }
}
.role-title {
  width: 152px;
  white-space: nowrap;
  word-break: break-all;
  word-wrap: break-word;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pageright {
  flex: 1;
}
.role-header {
  height: 64px;
  border-bottom: 1px solid #e0e0e0;
}
</style>